import axios from "axios";
import router from "../router";
import ElementUI from 'element-ui';
import nprogress from "nprogress"

let CancelToken  = axios.CancelToken;

let http = axios.create({
  baseURL:"/api",
  withCredentials:true,
  timeout: 1000*10,
})
http.interceptors.request.use(function (config) {
  nprogress.start()
  let rowPaths = ["/users/getCaptcha", "/users/login", "/users/verifyCaptcha"]
  if(rowPaths.includes(config.url)) {
      return config
  }else{
      let token = localStorage.getItem("lex-token") || "";
      config.headers['authorIzation'] = token;
      return config; 
  }
})  


http.interceptors.response.use(function(config){
  console.log(config)
  nprogress.done()
  if(config.data.code === "10022" || config.data.code === "1004") {
      router.push("/login");
      localStorage.removeItem("lex-token");
      location.reload();
      ElementUI.Message.error("登入过期,请重新登入")
      return
  }
  if(config.data.code === "10026") {
      ElementUI.Message.error("没有权限")
      return
  }
  if(!config.data.state && !config.data.img && config.config.url !== "/order/query_order_status"){
  ElementUI.MessageBox.alert(config.data.msg,"提示",{
      type:"error"
  })
  return config
}
return config;
})

export {
  http as
      default,
  CancelToken
};

